/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.min.js";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.multi-select.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";
import "../../../unit/common/js/bootstrap_js/form-components.js";

jQuery(document).ready(function() {
    App.init();
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }

    // 获取现有币种及汇率
    var param = 'CURR_TYP';
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/comParm/getByParmTyp/'+param,
        type: 'get',
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            console.log(resp);
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            $.each(resp.data,function(index,item){
                var html = '<tr class="saved" parmMemo="'+item.parmMemo+'" parmCod="'+item.parmCod+'" parmVal="'+item.parmVal+'">'+
                    '<td><input type="text" name="parmMemo" value="'+item.parmMemo+'" readonly="readonly"/></td>'+
                    '<td><input type="text" name="parmCod" value="'+item.parmCod+'" readonly="readonly"/></td>'+
                    '<td><input type="text" name="parmVal" value="'+item.parmVal+'" readonly="readonly"/></td>'+
                    '<td><a class="btn mini purple operate edit">编辑</a><a class="btn mini gray operate del">删除</a></td>'+
                    '</tr>';
                $(".currTypList").append(html);
            });
            bindEvent();
        },
        error:function(err){
            console.log(err);
        }
    });

    // 事件绑定
    function bindEvent(){
        // 编辑按钮
        $(".operate").unbind().on("click",function(){
            var trDom = $(this).parent().parent();
            var parmMemo = $(trDom).find("input[name='parmMemo']").val();
            var parmCod = $(trDom).find("input[name='parmCod']").val();
            var parmVal = $(trDom).find("input[name='parmVal']").val();

            if($(this).hasClass("edit")){  //  编辑
                // 点击编辑按钮时 设置input 框可编辑
                $(trDom).find("input[name='parmMemo']").attr("readonly",false);
                $(trDom).find("input[name='parmCod']").attr("readonly",true);
                $(trDom).find("input[name='parmVal']").attr("readonly",false);
                $(this).removeClass("purple edit").addClass("blue save").html("保存");
                // $(this).parent().parent().addClass("dirty");
                bindEvent();
            }
            if($(this).hasClass("save")){  // 保存
                var oldParmMemo = '',oldParmCod = '',oldParmVal = '';
                if($(trDom).hasClass("saved")){
                    oldParmMemo = $(trDom).attr("parmMemo");
                    oldParmCod = $(trDom).attr("parmCod");
                    oldParmVal = $(trDom).attr("parmVal");
                    if(parmMemo==oldParmMemo && oldParmVal==parmVal){
                        console.log("没有数据改动");
                        $(trDom).find(".operate.save").removeClass("blue save").addClass("purple edit").html("编辑");
                        $(trDom).find("input").attr("readonly",true);
                        return false;
                    }
                }
                if((oldParmCod!='' && oldParmCod==parmCod) || oldParmCod==''){
                    if(parmMemo==''){
                        alert("请填写币种");
                        return false;
                    }
                    if(isNaN(parmVal)){
                        alert("汇率必须是有效的数值");
                        return false;
                    }else{
                        var data={
                            "parmCod": parmCod,
                            "parmMemo": parmMemo,
                            "parmVal": parmVal,
                        };
                        $.ajax({
                            url: HOST_ADDR+'/cpmp/comParm/addCurrTyp',
                            type: 'post',
                            dataType: 'json',
                            contentType:"application/json",
                            headers: {
                                'access_token': token,'login_type':'ums'
                            },
                            data:JSON.stringify(data),
                            success: function(resp) {
                                console.log(resp);
                                if (!resp.success) {
                                    alert(resp.msgCod + ' ' + resp.msgInf);
                                    return;
                                }else{
                                    $(trDom).addClass("saved").removeClass("unsaved");
                                    $(trDom).attr("parmMemo",parmMemo);
                                    $(trDom).attr("parmCod",parmCod);
                                    $(trDom).attr("parmVal",parmVal);
                                    $(trDom).find(".operate.save").removeClass("blue save").addClass("purple edit").html("编辑");
                                    $(trDom).find("input").attr("readonly",true);
                                    alert("保存成功");
                                }
                                bindEvent();
                            },
                            error:function(err){
                                console.log(err);
                            }
                        });
                    }
                }
            }
            if($(this).hasClass("del")){ //删除
                if($(trDom).hasClass("saved")){
                    $.ajax({
                        url: HOST_ADDR+'/cpmp/comParm/deleteCurrTyp/'+parmCod,
                        type: 'get',
                        dataType: 'json',
                        contentType:"application/json",
                        headers: {
                            'access_token': token,'login_type':'ums'
                        },
                        data:JSON.stringify(data),
                        success: function(resp) {
                            console.log(resp);
                            if (!resp.success) {
                                alert(resp.msgCod + ' ' + resp.msgInf);
                                return;
                            }else{
                                $(trDom).remove();
                                alert("删除成功");
                            }
                            bindEvent();
                        },
                        error:function(err){
                            console.log(err);
                        }
                    });
                }else{
                    $(this).parent().parent().remove();
                }
            }
        });
    }

    // 添加币种汇率
    $(".toAdd").on("click",function(){
        var html = '<tr class="unsaved"  parmMemo="" parmCod="" parmVal="">'+
            '<td><input type="text" name="parmMemo" value="" /></td>'+
            '<td><input type="text" name="parmCod" value="" /></td>'+
            '<td><input type="text" name="parmVal" value="" /></td>'+
            '<td><a class="btn mini blue operate save">保存</a><a class="btn mini purple operate del">删除</a></td>'+
            '</tr>';
        $(".currTypList").append(html);
        bindEvent();
    });

});